// TOWN SCRIPT
//    Town 3: Deep Tunnel

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i, mnlvl, mxlvl, nlvl, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	if (get_flag(3, 0) == 0) {
		message_dialog("You seem to be a long abandoned tunnel leading who knows where.",
			"The dark and musty area makes you want to sneeze.");
		set_flag(3, 0, 1);
	}

	// balance monsters in this dungeon (behind the locked door)
	mnlvl = 200;
	mxlvl = 0;
	i = 0;
	// get min/max level in party
	while (i < 4) {
		nlvl = get_level(i);
		if (nlvl > mxlvl)
			mxlvl = nlvl;
		if (nlvl < mnlvl)
			mnlvl = nlvl;
		i = i + 1;
	}
	// balance rakshasa and mad monk level to party's level
	// first, make rakshasas same + 4 of max level (if less than 26)
	if (mxlvl < 26) {
		set_level(6, mxlvl + 4);
		set_level(9, mxlvl + 4);
	}
	// change level of mad monk to min level + 4 if min level < 20
	if (mnlvl < 20) {
		set_level(7, mnlvl + 4);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;

break;

beginstate 10;
	change_outdoor_location(3, 1, 44, 43);
break;

beginstate 11;
	change_outdoor_location(3, 2, 34, 3);
break;

beginstate 12;
	if (get_flag(3, 4) == 0) {
		reset_dialog();
		add_dialog_str(0, "As you search the bookcase, you notice a small crack behind one of the shelves.", 0);
		add_dialog_choice(0, "Leave it alone.");
		add_dialog_choice(1, "Pry it open.");
		choice = run_dialog(1);
		if (choice == 2) {
			play_sound(38);
			message_dialog("As you pry open the crack, a heavy iron key falls to the floor.", "You pick it up knowing it will be useful somewhere.");
			change_spec_item(1, 1);
			set_flag(3, 4, 1);
		}
	}
break;

beginstate 13;
	if (get_flag(3, 5) == 0) {
		reset_dialog();
		add_dialog_str(0, "It suddenly hits you what this place is.", 0);
		add_dialog_str(1, "It is one of the few secret passages the empire maintained into Avernum so they could send spies through to report on Avernum activities.", 0);
		add_dialog_str(2, "You know you have to be careful, because the empire used to keep powerful guardians in the passages in order to keep people from escaping, assuming they found them.", 0);
		add_dialog_str(3, "The dust has been disturbed recently by someone or something walking through it.", 0);
		add_dialog_choice(0, "OK.");
		choice = run_dialog(1);
		set_flag(3, 5, 1);
	}
break;

beginstate 14;
	if (get_flag(3, 6) == 0) {
		set_flag(3, 6, 1);
		message_dialog("Whoops, you've stumbled into some of the tunnel guardians left behind.", "They look angry...");
	}
break;
